/**
 * Odontosis - (2008 - Eclipse 3.3 - JDK1.6 Update 3) 
 */
package com.odontosis.dao;

import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.odontosis.as.OdontosisDataAccessObject;
import com.odontosis.entidade.Servico;
import com.odontosis.entidade.TipoServico;
import com.odontosis.util.HibernateUtil;
import com.odontosis.util.StringUtilsOdontosis;

/**
 * @author Oto
 * ltima modificao: $Author$<br>
 * Verso: $Revision$ $Date$
 */
public class ServicoDAO extends OdontosisDataAccessObject<Servico> {
	
	@SuppressWarnings("unchecked")
	public Collection<Servico> buscarServicoPorPacienteTipo(String nomePaciente, TipoServico tipoServico,String numeroPasta, boolean orcamento){
		HibernateUtil.closeSession();
		Session session = HibernateUtil.currentSession();
		Criteria criteria = session.createCriteria(Servico.class);
		criteria.createAlias("pacienteServico", "p");
		if(nomePaciente != null && !StringUtilsOdontosis.isVazia(nomePaciente)){
			criteria.add(Restrictions.like("p.nome", nomePaciente, MatchMode.ANYWHERE));
		}
		if(tipoServico != null){
			criteria.add(Restrictions.eq("tipoServico", tipoServico));
		}
		if(!StringUtilsOdontosis.isVazia(numeroPasta)){
			criteria.add(Restrictions.eq("p.numeroPasta", numeroPasta));
		}
		
		criteria.add(Restrictions.eq("orcamento", orcamento));
		criteria.add(Restrictions.eq("ativo", true));
		criteria.addOrder(Order.desc("dataCadastro"));
		criteria.setMaxResults(300);
		criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
		return criteria.list();
	}
	
	/**
	 * Método para retornar os dados do relaório serviços por período
	 * 
	 * @param dataInicial
	 * @param dataFinal
	 * @param servico
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Object[]> relatorioServicoPeriodo(Date dataInicial, Date dataFinal, boolean servico){
		HibernateUtil.closeSession();
		Session session = HibernateUtil.currentSession();
		String stringQuery = "select data_cadastro,tipo,valor,pago from servico "+
						" where orcamento = :orcamento" +
						" and data_cadastro between :dataInicial and :dataFinal";
		Query query  = session.createSQLQuery(stringQuery);
		query.setParameter("orcamento", !servico);
		query.setParameter("dataInicial", dataInicial);
		query.setParameter("dataFinal", dataFinal);
		return query.list();
	}
	
	@SuppressWarnings("unchecked")
	public List<Servico> relatorioServicoPorPeriodo(Date dataInicial, Date dataFinal, boolean servico){
		HibernateUtil.closeSession();
		Session session = HibernateUtil.currentSession();
		Criteria criteria = session.createCriteria(Servico.class);
		criteria.add(Restrictions.between("dataCadastro", dataInicial, dataFinal));
		criteria.add(Restrictions.eq("orcamento", !servico));
		criteria.add(Restrictions.eq("ativo", true));
		criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
		criteria.addOrder(Order.asc("dataCadastro"));
		return criteria.list();
	}
	
	
			
	
}